Imports ccapps.persistent.authorization
Imports System.Data.OracleClient.OracleConnection

Namespace Web.page
    Public Class menu
        Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub
        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object


        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

#End Region

        'Private _MenuJavaScriptLibrary As persistent.ScriptLibrary.LibraryFile = _
        'persistent.ScriptLibrary.LibraryFile.ReadLibraryFile(Server.MapPath("/ccapps/persistent/ScriptLibrary/Javascript/Menu.js"))

        'Private _MenuCSSLibrary As persistent.ScriptLibrary.LibraryFile = _
        'persistent.ScriptLibrary.LibraryFile.ReadLibraryFile(Server.MapPath("/ccapps/persistent/ScriptLibrary/CSS/Menu.css"))

        'Private __MenuLevelInitScript As String = _
        '_MenuJavaScriptLibrary.GetBlock("MENU_SUBLEVEL_INIT")

        'Private __MenuLevelFormatCSS As String = _
        '_MenuCSSLibrary.GetBlock("MENU_SUBLEVEL_STYLE")



        Private mainMenuTemplateHTML As String = _
        vbNewLine & "<h3 class=""menuheader expandable"">[MENU_TITLE]</h3>" & _
        vbNewLine & "<ul class=""categoryitems"">" & _
                    "[SUB_MENU_HTML]" & _
        vbNewLine & "</ul>"

        Private menuGroupTemplateHTML As String = _
        vbNewLine & "[PADDING]<li><a href=""[MENU_LINK]"" class=""sub[LEVEL_NUMBER]expandable"" target=""anaframe"">[MENU_TITLE]</a>" & _
        vbNewLine & "[PADDING]    <ul class=""sub[LEVEL_NUMBER]categoryitems"" style=""MARGIN-LEFT: 15px"">" & _
                    "[SUB_MENU_HTML]" & _
        vbNewLine & "[PADDING]     </ul>" & _
        vbNewLine & "[PADDING]</li>"

        Private menuItemTemplateHTML As String = _
        vbNewLine & "[PADDING]<li><a href=""[MENU_LINK]"" target=""anaframe"" onclick=""onMenuClick(this);"">[MENU_TITLE]</a></li>"

        Private maxLevelNumber As Integer



        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            If (Session("user") = "") Then
                Response.Write("<SCRIPT type='text/javascript'> ")
                Response.Write(" parent.location.href='/ccapps/login.aspx';")
                Response.Write("</SCRIPT> ")
                Exit Sub
            End If





            Dim renderOutput As String = String.Empty
            Dim subMenusRenderOutput As String = String.Empty


            Dim dtMenu As DataTable = Get_menu(Session("user"))
            Dim mainMenus As New DataView(dtMenu, "PARENT_MENU_ID IS NULL", "DISPLAY_ORDER", DataViewRowState.CurrentRows)
            For Each dr As DataRowView In mainMenus

                If IsDBNull(dr("MENU_TITLE")) Then
                    dr("MENU_TITLE") = String.Empty
                End If

                If IsDBNull(dr("MENU_LINK")) Then
                    dr("MENU_LINK") = String.Empty
                End If


                subMenusRenderOutput = renderSubMenus(dtMenu, dr("MENU_ID"), 1)

                renderOutput &= mainMenuTemplateHTML. _
                Replace("[MENU_TITLE]", dr("MENU_TITLE")). _
                Replace("[SUB_MENU_HTML]", subMenusRenderOutput)

            Next

            For levelIndex As Integer = 1 To maxLevelNumber - 1

                'RegisterScript _
                '( _
                '    String.Format(__MenuLevelInitScript, levelIndex), _
                '    Me _
                ')

                'writeContent _
                '( _
                '    String.Format _
                '    ( _
                '        Me.__MenuLevelFormatCSS, _
                '        levelIndex _
                '    ) _
                ')

            Next


            writeContent("<body>")

            writeContent("<div class=""arrowlistmenu"">")

            writeContent(renderOutput)

            writeContent("</div>")


            Response.Write("<br><table  valign=top cellspacing=0 cellpadding=0 width=200>")
            Response.Write("<tr><td height=20 valign=middle align=center><a onclick='javascript:parent.frames.topframe.Vgenel=1;parent.location.href=""/ccapps/web/menu/logout.aspx""';><img border=0 src='/ccapps/web/images/cikis7.gif' style='cursor:hand' onmouseover=this.src='/ccapps/web/images/cikis8.gif' onmouseout=this.src='/ccapps/web/images/cikis7.gif'  /></a></td></tr></table>")

            writeContent(" </body></HTML>")

            Response.Flush()



        End Sub

        Private Function renderSubMenus(ByVal dtMenu As DataTable, ByVal parentMenuID As Integer, Optional ByRef levelNumber As Integer = 1) As String
            Dim subMenus As New DataView(dtMenu, "PARENT_MENU_ID = " & parentMenuID, "HAS_CHILDREN, MENU_TITLE", DataViewRowState.CurrentRows)
            Dim paddingString = cloneString("        ", levelNumber)
            maxLevelNumber = levelNumber

            Dim renderOutput As String = String.Empty
            Dim subMenusRenderOutput As String = String.Empty

            For Each dr As DataRowView In subMenus

                If IsDBNull(dr("MENU_TITLE")) Then
                    dr("MENU_TITLE") = String.Empty
                End If

                If IsDBNull(dr("MENU_LINK")) Then
                    dr("MENU_LINK") = String.Empty
                End If

                If CInt(dr("HAS_CHILDREN")) = 1 Then

                    subMenusRenderOutput = renderSubMenus(dtMenu, dr("MENU_ID"), levelNumber + 1)

                    renderOutput &= menuGroupTemplateHTML. _
                    Replace("[PADDING]", paddingString). _
                    Replace("[MENU_LINK]", dr("MENU_LINK")). _
                    Replace("[LEVEL_NUMBER]", levelNumber). _
                    Replace("[MENU_TITLE]", dr("MENU_TITLE")). _
                    Replace("[SUB_MENU_HTML]", subMenusRenderOutput)

                Else
                    renderOutput &= menuItemTemplateHTML. _
                    Replace("[PADDING]", paddingString). _
                    Replace("[MENU_LINK]", dr("MENU_LINK")). _
                    Replace("[MENU_TITLE]", dr("MENU_TITLE"))

                End If

            Next

            Return renderOutput
        End Function

        Private Function cloneString(ByVal stringToClone As String, ByVal cloneCount As Integer) As String
            Return String.Empty.PadLeft(stringToClone.Length * cloneCount, stringToClone)
        End Function

        'Private Function hasChildren(ByVal dtMenu As DataTable, ByVal parentMenuID As Integer) As Boolean
        '    Return New DataView(dtMenu, "PARENT_MENU_ID = " & parentMenuID, Nothing, DataViewRowState.CurrentRows).Count > 0
        'End Function

        Public Shared Function Get_menu(ByVal user As String) As DataTable
            Dim SQL As String

            SQL = "select c.*, case when (select count(*) from billintra.menu2 c2 where c2.parent_menu_id = c.menu_id and enabled = 'Y') > 0 then 1 else 0 end has_children " & _
                vbNewLine & "from billintra.login_user a, billintra.menu_permissions b, billintra.menu2 c " & _
                vbNewLine & "where a.userid='" & user & "' " & _
                vbNewLine & "and a.role_id=b.role_id " & _
                vbNewLine & "and c.menu_id = b.menu_id " & _
                vbNewLine & "and c.enabled='Y' " & _
                vbNewLine & "order by parent_menu_id nulls first, display_order, menu_title"

            Return persistent.myDBConn.myORATable(SQL)
        End Function

        Private Sub writeContent(ByVal content As String)
            Response.Write(content & vbNewLine)
        End Sub
    End Class

End Namespace